<?php
/**
 * 租赁预约
 * @author cjli
 *
 */
class TenancyAction extends AdminAction
{
	public function _initialize(){
		parent::_initialize();
	}
	
	public function lists(){
		$where = array();
		if(isset($_POST['search'])){
			if(isset($_POST['name']) && $_POST['name']){
				$where['name'] = I('post.name',trim);
			}
			if(isset($_POST['phone']) && $_POST['phone']){
				$where['phone'] = I('post.phone',trim);
			}
			if(isset($_POST['start_time']) && $_POST['start_time']){
				$where['sub_time'] = array('gt',strtotime(trim($_POST['start_time'])));
			}
			if(isset($_POST['end_time']) && $_POST['end_time']){
				$where['sub_time'] = array('lt',strtotime(trim($_POST['end_time'])));
			}
		}
		
		$ten_db = D('tenancy');
		$ten_db->where($where);
		$count = $ten_db->count();//总记录数
		import("ORG.Util.Page");// 导入分页类
		$Page = new Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数
		$pageShow = $Page->show();// 分页显示输出
		$this->assign('pageShow', $pageShow);
		//预约列表
		$ten_db->limit($Page->firstRow.','.$Page->listRows);
		$ten_db->field('*');
		$ten_db->where($where);
		$ten_db->order('statue ASC,createtime DESC');
		$tenList = $ten_db->select();
		
		$this->assign("statues",C("HANDLE_STATUE"));
		$this->assign('tenList', $tenList);
		
		$this->display('lists');
	}
	
	public function info_edit(){
		$id  = 	I("get.id",intval);
		$ten_db = D('tenancy');
		$ten_db->field('*');
		$tenInfo = $ten_db->where('id='.$id)->find();
		if(!$tenInfo){
			$this->error('预约不存在！', U('tenancy/lists'), 'tenancy');
		}
        
		if($_POST['dosubmit']){
			$where=array();
			$where['id'] = I('post.id',intval);
			$data = array(
				'statue' => I('post.statue',intval),
				'content' => I('post.content',intval)
			);
	
			if($ten_db->where($where)->save($data)){
				$this->success('操作成功', U('tenancy/lists'));
			}else{
				$this->error('操作失败', U('tenancy/lists'));
			}
		}else{
			$this->assign('tenInfo', $tenInfo);
			$this->display('info_edit');
		}	
	}
	
	public function info_delete(){
		$id = I("get.id",intval);
		$ids = $_POST['ids'];
		$ten_db = D('tenancy');
		if($id){
			$ten_db->delete($id);
		}if(!empty($ids)){
			$ids = implode(',', $ids);
			$ten_db->delete($ids);
		}
		redirect(U('tenancy/lists'));	
	}
}